package com.example.a2007dou_shop.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.example.a2007dou_shop.R
import com.example.a2007dou_shop.adapter.CommentAdapter
import com.example.a2007dou_shop.adapter.VideoAdapter
import com.example.a2007dou_shop.databinding.FragmentVideosBinding
import com.example.a2007dou_shop.entity.CommentEntity
import com.example.a2007dou_shop.entity.VideosEntity
import com.example.a2007dou_shop.mvvm.BaseFragment
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer


class VideosFragment : BaseFragment<FragmentVideosBinding,VideosViewModel>() {
    val list = mutableListOf<VideosEntity>()
    lateinit var videoAdapter: VideoAdapter

    val commentlist = mutableListOf<CommentEntity>()
    lateinit var commentAdapter: CommentAdapter
    override fun getLayout(): Int = R.layout.fragment_videos

    override fun getViewModel(): Class<VideosViewModel>  = VideosViewModel::class.java

    override fun initView() {
        videoAdapter = VideoAdapter(list)
        binding.rv.adapter = videoAdapter
        val linearLayoutManager = LinearLayoutManager(context)
        binding.rv.layoutManager = linearLayoutManager
        //优化分页滑动
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(binding.rv)
        //滑动自动播放
        binding.rv.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                var position = linearLayoutManager.findFirstVisibleItemPosition()
                val gsy = videoAdapter.getViewByPosition(position,R.id.gsy) as StandardGSYVideoPlayer
                gsy.startPlayLogic()
            }
        })
        viewModel.getVideos(1,50)
        //点击跳转到详情界面
        videoAdapter.addChildClickViewIds(R.id.iv_user,R.id.iv_pinglun,R.id.comment_close,R.id.comment_send)
        videoAdapter.setOnItemChildClickListener {  adapter, view, position ->
            /*ARouter.getInstance().build("/app/DetailActivity").withSerializable("video",list[position]).navigation()*/
            when(view.id){
                R.id.iv_user->{
                    ARouter.getInstance().build("/app/DetailActivity").withSerializable("video",list[position]).navigation()
                }
                R.id.iv_pinglun->{
                    //打开评论框----加载评论内容
                    binding.rv.flag = false
                    openComment(position)
                }
                R.id.comment_close->{
                    binding.rv.flag = true
                    closeComment(position)
                }
                R.id.comment_send->{
                    sendComment(position)
                }
            }

        }
    }

    private fun sendComment(position: Int) {
        //找到输入内容---找到输入框控件
        val commentEt = videoAdapter.getViewByPosition(position,R.id.comment_et) as EditText
        viewModel.sendComment(list[position].id,commentEt.text.toString())
        commentEt.setText("")
    }

    private fun closeComment(position: Int) {
        //找到评论框
        val commentcl = videoAdapter.getViewByPosition(position,R.id.comment_cl) as ConstraintLayout
        commentcl.isVisible = false
    }

    private fun openComment(position: Int) {
        //找到评论框
        val commentcl = videoAdapter.getViewByPosition(position,R.id.comment_cl) as ConstraintLayout
        commentcl.isVisible = true
        //加载评论数据
        //找到rv进行数据绑定
        val commentrv = videoAdapter.getViewByPosition(position,R.id.comment_rv) as RecyclerView
        commentAdapter = CommentAdapter(commentlist)
        commentrv.adapter = commentAdapter
        commentrv.layoutManager = LinearLayoutManager(context)
        viewModel.getComment(list[position].id)

    }

    override fun initData() {
        viewModel.videoDui.observe(this){
            if(it.code==200){
                list.clear()
                list.addAll(it.data)
                videoAdapter.notifyDataSetChanged()
            }
        }
        viewModel.getCommentDui.observe(this){
            if(it.code==200){
                commentlist.clear()
                commentlist.addAll(it.data)
                commentAdapter.notifyDataSetChanged()
            }
        }
        viewModel.sendCommentDui.observe(this){
            if(it.code==200){
                commentlist.add(0,it.data[0])
                commentAdapter.notifyDataSetChanged()
            }
        }

    }

}